package fragments;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.example.junior.activities.R;

import java.util.List;

import basicas.AoClicarNoTimeListener;
import basicas.Time;
import repositorio.TimesDB;
import util.TimesAdapter;

/**
 * Created by Junior on 28/10/2014.
 */
public class ListaFavoritoFragment extends Fragment {

    List<Time> timeList;
    ListView minhaListView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        atualizarTela();

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(
                R.layout.fragment_time_favorito,container,false);
        minhaListView = (ListView) view.findViewById(R.id.lista);
        minhaListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Time time = timeList.get(i);
                notificarListener(time, true);
            }
        });
        return view;
    }

    public void notificarListener(Time time, boolean b) {
        if (getActivity() instanceof AoClicarNoTimeListener) {
            AoClicarNoTimeListener listener =
                    (AoClicarNoTimeListener) getActivity();
            listener.aoClicarNoTime(time, b);
        }
    }

    public void atualizarTela() {

        TimesDB db = new TimesDB(getActivity());
        timeList = db.listarTime();

        TimesAdapter adapter = new TimesAdapter(getActivity(), timeList);
        minhaListView.setAdapter(adapter);
    }

}
